package com.parent.api.aliyun;

import cn.hutool.json.JSONUtil;
import com.aliyun.ocr_api20210707.models.*;
import com.aliyun.tea.TeaException;
import com.parent.api_entity.aliyun.OCREntity;
import com.parent.api_entity.aliyun.OCRIdentityCard;


import java.io.InputStream;
import java.util.Objects;

/**
 * @Author yan
 * @Date 2023/6/28 0028 13:45
 * @Description
 */
public class OCRUtils {

    /**
     * ocr 的
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @param endpoint        --域名
     * @return
     * @throws Exception
     */
    public static com.aliyun.ocr_api20210707.Client createClient(String accessKeyId, String accessKeySecret, String endpoint) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = endpoint;
        return new com.aliyun.ocr_api20210707.Client(config);
    }

    /**
     * 阿里云 每月免费200次
     * 图片文字识别
     * @param accessKeyId
     * @param accessKeySecret
     * @param endpoint
     * @param input
     * @return
     * @throws Exception
     */
    public static OCREntity ocrText(String accessKeyId, String accessKeySecret, String endpoint, InputStream input) throws Exception {
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.ocr_api20210707.Client client = createClient(accessKeyId, accessKeySecret, endpoint);
        com.aliyun.ocr_api20210707.models.RecognizeAdvancedRequest ocr = new com.aliyun.ocr_api20210707.models.RecognizeAdvancedRequest();

        ocr.setBody(input);

        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();

        OCREntity ocrEntity = null;
        try {
            // 复制代码运行请自行打印 API 的返回值
            RecognizeAdvancedResponse response = client.recognizeAdvancedWithOptions(ocr, runtime);
            //System.out.println(response.toString());

            RecognizeAdvancedResponseBody body = response.getBody();
            String data = body.getData();
//            System.out.println(data);
//            JSONObject jsonObject = JSON.parseObject(data);
//            String content = jsonObject.getString("content");
//            System.out.println(content);
            ocrEntity = JSONUtil.toBean(data, OCREntity.class);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            //com.aliyun.teautil.Common.assertAsString(error.message);
            throw error;
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            //com.aliyun.teautil.Common.assertAsString(error.message);
            throw error;
        }

        return ocrEntity;
    }

    public static OCREntity ocrText(String accessKeyId, String accessKeySecret, String endpoint, String url) throws Exception {
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.ocr_api20210707.Client client = createClient(accessKeyId, accessKeySecret, endpoint);
        com.aliyun.ocr_api20210707.models.RecognizeAdvancedRequest ocr = new com.aliyun.ocr_api20210707.models.RecognizeAdvancedRequest();

        ocr.setUrl(url);

        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();

        OCREntity ocrEntity = null;
        try {
            // 复制代码运行请自行打印 API 的返回值
            RecognizeAdvancedResponse response = client.recognizeAdvancedWithOptions(ocr, runtime);
            //System.out.println(response.toString());

            RecognizeAdvancedResponseBody body = response.getBody();
            String data = body.getData();
//            System.out.println(data);
//            JSONObject jsonObject = JSON.parseObject(data);
//            String content = jsonObject.getString("content");
//            System.out.println(content);
            ocrEntity = JSONUtil.toBean(data, OCREntity.class);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            //com.aliyun.teautil.Common.assertAsString(error.message);
            throw error;
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            //com.aliyun.teautil.Common.assertAsString(error.message);
            throw error;
        }

        return ocrEntity;
    }


    /**
     * 通用文字识别
     * @param accessKeyId
     * @param accessKeySecret
     * @param endpoint
     * @param url
     * @return
     * @throws Exception
     */
    public static OCREntity ocrTextCommon(String accessKeyId, String accessKeySecret, String endpoint, String url,InputStream input) throws Exception {
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.ocr_api20210707.Client client = createClient(accessKeyId, accessKeySecret, endpoint);
        com.aliyun.ocr_api20210707.models.RecognizeGeneralRequest ocr = new com.aliyun.ocr_api20210707.models.RecognizeGeneralRequest();
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        OCREntity ocrEntity = null;
        try {
            // 复制代码运行请自行打印 API 的返回值
            if (Objects.nonNull(url)){
                ocr.setUrl(url);
            }else {
                ocr.setBody(input);
            }
            RecognizeGeneralResponse response = client.recognizeGeneralWithOptions(ocr, runtime);
            RecognizeGeneralResponseBody body = response.getBody();
            String data = body.getData();
            ocrEntity = JSONUtil.toBean(data, OCREntity.class);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return ocrEntity;
    }

    /**
     * 身份证识别 阿里云 每月免费200次
     * @param accessKeyId
     * @param accessKeySecret
     * @param endpoint
     * @param input
     * @return
     * @throws Exception
     */
    public static OCRIdentityCard ocrIdentityCard(String accessKeyId, String accessKeySecret, String endpoint, InputStream input) throws Exception {
        com.aliyun.ocr_api20210707.Client client = createClient(accessKeyId, accessKeySecret, endpoint);
        com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest ocr = new com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest();

        ocr.setBody(input);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        // 复制代码运行请自行打印 API 的返回值
        RecognizeIdcardResponse response = client.recognizeIdcardWithOptions(ocr, runtime);

        RecognizeIdcardResponseBody body = response.getBody();
        String data = body.getData();
//        System.out.println(data);
//        System.out.println("=========");
//        JSONObject jsonObject = JSON.parseObject(data);
//        JSONObject data1 = jsonObject.getJSONObject("data");
//        JSONObject face = data1.getJSONObject("face");
//        JSONObject object = face.getJSONObject("data");
//
//        String address = object.getString("address");
//        String name = object.getString("name");
//        String ethnicity = object.getString("ethnicity");
//        String idNumber = object.getString("idNumber");
//        String sex = object.getString("sex");
//        String birthDate = object.getString("birthDate");
        OCRIdentityCard ocrIdentityCard = JSONUtil.toBean(data, OCRIdentityCard.class);
        return ocrIdentityCard;
    }

}
